<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Subscriptions extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
        $this->check_authentication(1);
        $this->load->model('Candidate_model', 'candidate');
        $this->load->model('Subscription_model', 'subscription');
        $this->lang->load('candidate', $this->session->userdata['user_lang']);
        $this->lang->load('candidate/subscriptions', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$data['main_content'] = 'candidate/subscriptions';
        $data['userdata'] = $this->session->userdata;
        
        $array_search = array('idcandidate' => $this->session->userdata['idprofile'], 'idstatus' => array(1,2,3,4));
        $data['grid_subscription_available'] = $this->get_grid($this->subscription->get($array_search));
        
        $array_search = array('idcandidate' => $this->session->userdata['idprofile'], 'idstatus' => array(5));
        $data['grid_subscription_unavailable'] = $this->get_grid($this->subscription->get($array_search));
        
        $this->load->view('template-candidate', $data);
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('address_missingfield'));
		}
		
		if ($this->subscription->delete(array('id' => $data['id'], 'idcandidate' => $this->session->userdata['idprofile'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('$("#subscription'.$data['id'].'").remove();');
		}
	}
	
	private function get_grid($job_list)
	{
		$grid_jobs = array();
        
        foreach ($job_list->result() as $row)
		{
			switch ($row->idstatus)
			{
				case 2:
					$status = $this->lang->line('subscriptions_seen');
					break;
				case 3:
					$status = $this->lang->line('subscriptions_evaluation');
					break;
				case 4:
					$status = $this->lang->line('subscriptions_finalist');
					break;
				case 5:
					$status = $this->lang->line('subscriptions_discarded');
					break;
				case 1:
				default:
					$status = $this->lang->line('subscriptions_unseen');
					break;
			}
			
			$grid_jobs[] = '<div class="row" id="subscription'.$row->id.'">
				<span>
					<span class="gtitle">
						<a href="'.base_url().'job/detail/'.$row->id.'/'.str_replace(' ', '-', $row->title).'">' . $row->title . '</a>
						('.$status.')
					</span>'
			.'<br/><span class="gdesc">'.format_date($row->date, true) . ' - ' . $row->company 
			. '</span></span>
			<div class="gtool">
				<a href="'.base_url().'job/detail/'.$row->id.'/'.str_replace(' ', '-', $row->title).'">'.$this->lang->line('candidate_viewjob').'</a>
				&middot; <a onclick="Candidate.deleteSubscription('.$row->id.');">'.$this->lang->line('candidate_deletesubscription').'</a>
			</div>
			</div>';
		}
		
		if (!$grid_jobs) {
			 $grid_jobs = '<center>'.$this->lang->line('subscriptions_noresults').'</center>';
		} else {
			 $grid_jobs = implode('<hr/>', $grid_jobs);
		}
		
		return $grid_jobs;
	}
}

?>